const express = require("express")
const fs = require("fs")


const app = express()

function myStatic(dir) {
    return (req, res, next) => {
        console.log("myStatic")
        let path = req.path 
        if(path === "/") {
            path = "/index.html"
        }
        const filePath = __dirname + "/" +dir + path
        fs.readFile(filePath, (error, data) => {
            if(error) {
                next()
            } else {
                res.send(data.toString(

                ))
            }
        })
    }
}
app.use(myStatic("public"))

app.use(express.urlencoded({extended: false}))
app.use(express.json())

app.get("/middleware", (req,res) => {
    res.send("middleware response")
})
app.post("/middleware", (req,res) => {
    const {b, c} = req.body
    console.log("请求体参数",b, c)
    res.send("post middleware response")
})

app.listen("8080", () => {
    console.log("服务已启动: http://127.0.0.1:8080");
})